package com.aop.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

//@Component
//@Aspect
public class MyAspect {

    //定义切入点
    @Pointcut("execution(* com.aop.service.*.*(..))")
    private void pointCut(){}

    @Before("pointCut()")
    public void doBefore(){
        System.out.println("前置通知...Before");
    }

    @AfterReturning("pointCut()")
    public void doAfterReturning(){
        System.out.println("返回通知...AfterReturning");
    }

    @AfterThrowing("pointCut()")
    public void afterThrowing(){
        System.out.println("异常通知...AfterThrowing");
    }

    @After("pointCut()")
    public void doAfter(){
        System.out.println("后置通知(最终)...After");
    }

    //@Around("execution(* com.aop.service.*.add(..))")
    public Object around(ProceedingJoinPoint joinPoint){
        System.out.println("开始");
        try {
            //获取方法参数
            System.out.println("传入参数:" +Arrays.toString(joinPoint.getArgs()));
            Object result = joinPoint.proceed();
            System.out.println("执行结果:" + result);

            return (int)result + 1;
        } catch (Throwable throwable) {
            throw new RuntimeException(throwable);
        }finally {
            System.out.println("结束");
        }
    }
}
